package com.songlanyun.modules.servicegoods.controller.api;

import cn.hutool.core.map.MapUtil;
import com.songlanyun.common.annotation.RepeatSubmit;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.goods.model.dto.GoodsStatusDTO;
import com.songlanyun.modules.goods.model.vo.GoodsSlimVo;
import com.songlanyun.modules.servicegoods.entity.ServiceGoods;
import com.songlanyun.modules.servicegoods.model.dto.ServiceGoodsDTO;
import com.songlanyun.modules.servicegoods.model.vo.NearbyShopSlimVo;
import com.songlanyun.modules.servicegoods.model.vo.ServiceGoodsSlimVo;
import com.songlanyun.modules.servicegoods.service.ServiceGoodsService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 服务商品
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2020-12-15 11:33:21
 */
@RestController
@RequestMapping("api/service/goods")
public class ServiceGoodsApiController extends AbstractApiController {
    @Autowired
    private ServiceGoodsService serviceGoodsService;


    /**
     * 服务商品id查看商品
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("servicegoods:servicegoods:info")
    public R info(@PathVariable("id") Long id) {
        ServiceGoods serviceGoods = serviceGoodsService.loadServiceGoodsById(id,true);
        serviceGoods.loadCategory();
        serviceGoods.loadGroupList();
        serviceGoods.loadContent();
        serviceGoods.loadServiceItems();
        return R.ok().put("data", serviceGoods);
    }



    /**
     * 分页查询商家的服务商品
     *  * params参数说明
     *      * shop_id 商家店铺id可以不给不给查所有
     *      * verify_status 审核状态 可以不给查全部 有效值：（0待审，1通过，2不通过）
     *      * group_id 商家分组
     *      * added 上下架
     *      * title 标题（可以不给，用来模糊查询）
     *      * page 当前第几页（默认第一页）
     *      * limit 每页多少条 （默认每页10条）
     */
    @RequestMapping("/self/pages/{shopId}")
    //@RequiresPermissions("goods:all:list")
    public R selfGoodsList(@PathVariable("shopId") Long shopId,@RequestParam Map<String, Object> params){
        params.put("shop_id",shopId);
        params.put("added",1);
        params.put("verify_status",1);
        PageUtils page = serviceGoodsService.querySelfPage(params);
        return R.ok().put("page", page);
    }



    /**
     * 小程序
     * 在商家店铺中显示推荐，热门，新品，自营商品
     * type = recommend，hot,new
     * limit= 数量
     *
     * APP
     * 在商家店铺中显示商品列表
     * 商家店铺中商品列表(直显示直销商品，不包括代销的平台商品)
     */
    @RequestMapping("/list/{shopId}")
    public R showGoodsList(@PathVariable("shopId") Long shopId,@RequestParam Map<String, Object> params){
        params.put("shop_id",shopId);
        Integer limit = MapUtil.getInt(params, "limit");
        if(limit==null)
        {
            params.put("limit",8);
        }
        List<ServiceGoodsSlimVo> list =  serviceGoodsService.loadList(params);
        return R.ok().put("list", list);
    }


    /**
     * APP
     * 在商家店铺中显示服务商品数量
     */
    @RequestMapping("/count/{shopId}")
    public R showGoodsCount(@PathVariable("shopId") Long shopId,@RequestParam Map<String, Object> params){
        params.put("shop_id",shopId);
        Integer count =  serviceGoodsService.loadCount(params);
        return R.ok().put("data", count);
    }



    /**
     *
     * APP
     * 附近的门店
     */
    @RequestMapping("/nearby")
    public R nearbyShopList(@RequestParam Map<String, Object> params){
        Integer limit = MapUtil.getInt(params, "limit");
        if(limit==null)
        {
            params.put("limit",100);
        }
        List<NearbyShopSlimVo> list =  serviceGoodsService.nearbyShopList(params);
        return R.ok().put("list", list);
    }


}
